<template>
  <div class="container">
    <el-card>
      <div class="q_title">
        <span style="color: pink; font-size: 12px">by 于亦浓</span>
        <span style="color: pink; font-size: 12px"
          >说明：目前支持学科和关键字条件筛选</span
        >
        <el-button type="success" icon="el-icon-edit" size="small" @click="$router.push('new')"
          >新增试题</el-button
        >
      </div>
      <el-form :model="searchForm" ref="searchFormRefs" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="学科" prop="subjectID">
              <el-select
                v-model="searchForm.subjectID"
                placeholder="请选择"
                size="small"
                @change="selectChanged"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item in subjectList"
                  :key="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录" prop="catalogID">
              <el-select
                v-model="searchForm.catalogID"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item in secondList"
                  :key="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签" prop="tags">
              <el-select
                v-model="searchForm.tags"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item in tagsList"
                  :key="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字" prop="keyword">
              <el-input v-model="searchForm.keyword" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="试题类型" prop="questionType">
              <el-select
                v-model="searchForm.questionType"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item in questionType"
                  :key="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度" prop="difficulty">
              <el-select
                v-model="searchForm.difficulty"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item in difficulty"
                  :key="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向" prop="direction">
              <el-select
                v-model="searchForm.direction"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  :label="item"
                  :value="item"
                  v-for="(item, index) in direction"
                  :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人" prop="creatorID">
              <el-select
                v-model="searchForm.creatorID"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  :label="item.username"
                  :value="item.id"
                  v-for="item in userList"
                  :key="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="题目备注" prop="remarks">
              <el-input v-model="searchForm.remarks" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称" prop="shortName">
              <el-input v-model="searchForm.shortName" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-select
                v-model="searchForm.province"
                placeholder="请选择"
                size="small"
                style="width: 48%; margin-right: 2%"
                @change="selectListChanged"
              >
                <el-option
                  :label="item"
                  :value="item"
                  v-for="(item, index) in provinceList"
                  :key="index"
                >
                </el-option>
              </el-select>
              <el-select
                v-model="searchForm.city"
                placeholder="请选择"
                size="small"
                style="width: 50%"
              >
                <el-option
                  :label="item"
                  :value="item"
                  v-for="(item, index) in cityList"
                  :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="text-align: right">
              <el-button size="small" @click="clear">清除</el-button>
              <el-button type="primary" size="small" @click="getQuestionList()"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick">
        <el-tab-pane label="全部" name="first">
          <el-alert
            :title="'数据一共 ' + total + ' 条'"
            type="info"
            show-icon
            :closable="false"
            style="margin-bottom: 15px"
          >
          </el-alert>
          <el-table :data="questionList" border stripe style="width: 100%">
            <el-table-column prop="number" label="试题编号" width="150">
            </el-table-column>
            <el-table-column prop="subject" label="学科" width="120">
            </el-table-column>
            <el-table-column prop="catalog" label="目录" width="120">
            </el-table-column>
            <el-table-column prop="questionType" label="题型" width="120">
              <template v-slot="{ row }">
                <div v-if="row.questionType === '1'">单选</div>
                <div v-else-if="row.questionType === '2'">多选</div>
                <div v-else>简答</div>
              </template>
            </el-table-column>
            <el-table-column prop="question" label="题干" width="300">
              <template slot-scope="scope">
                <div v-html="scope.row.question"></div>
              </template>
            </el-table-column>
            <el-table-column prop="addDate" label="录入时间" width="160">
              <template v-slot="{ row }">
                {{ row.addDate | parseTimeByString }}
              </template>
            </el-table-column>
            <el-table-column prop="difficulty" label="难度" width="120">
              <template v-slot="{ row }">
                <div v-if="row.difficulty === '1'">简单</div>
                <div v-else-if="row.difficulty === '2'">一般</div>
                <div v-else>困难</div>
              </template>
            </el-table-column>
            <el-table-column prop="creator" label="录入人" width="120">
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center">
              <template slot-scope="scope">
                <!-- 预览 -->
                <el-button
                  @click="getPreviewObj(scope.row)"
                  type="primary"
                  size="small"
                  circle
                  plain
                  icon="el-icon-view"
                ></el-button>
                <!-- 编辑 -->
                <el-button
                  type="success"
                  size="small"
                  circle
                  plain
                  icon="el-icon-edit"
                  @click="$router.push('new')"
                ></el-button>
                <!-- 删除 -->
                <el-button
                  type="danger"
                  size="small"
                  @click="delTopic(scope.row)"
                  circle
                  plain
                  icon="el-icon-delete"
                ></el-button>
                <!-- 加精 -->
                <el-button
                  type="warning"
                  size="small"
                  @click="addPerfect(scope.row)"
                  circle
                  plain
                  icon="el-icon-check"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <el-pagination
        style="text-align: right; margin-top: 15px"
        @size-change="handleSizeChange"
        background
        @current-change="handleCurrentChange"
        :current-page="questionInfo.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="questionInfo.pagesize"
        layout="prev,pager,next,sizes,jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      title="题目预览"
      :visible.sync="topicDialogVisible"
      width="55%"
      @close="topicDialogClose"
    >
      <el-row>
        <el-col :span="6" style="margin: 10px 0">
          <div v-if="previewobj.questionType === '1'">【题型】：单选题</div>
          <div v-else-if="previewobj.questionType === '2'">
            【题型】：多选题
          </div>
          <div v-else>【题型】：简答题</div>
        </el-col>
        <el-col :span="6" style="margin: 10px 0">
          【编号】：{{ previewobj.id }}
        </el-col>
        <el-col :span="6" style="margin: 10px 0">
          <div v-if="previewobj.difficulty === '1'">【题型】：简单</div>
          <div v-else-if="previewobj.difficulty === '2'">【题型】：一般</div>
          <div v-else>【题型】：困难</div>
        </el-col>
        <el-col :span="6" style="margin: 10px 0">
          【标签】：{{ previewobj.tags }}
        </el-col>
        <el-col :span="6" style="margin: 10px 0">
          【学科】：{{ previewobj.subjectName }}
        </el-col>
        <el-col :span="6" style="margin: 10px 0">
          【目录】：{{ previewobj.directoryName }}
        </el-col>
        <el-col :span="6" style="margin: 10px 0">
          【方向】：{{ previewobj.direction }}
        </el-col>
      </el-row>
      <hr />
      <div>【题干】：</div>
      <div v-html="previewobj.question" style="color: blue"></div>
      <div v-if="previewobj.questionType === '1'">
        单选题 选项：（以下选中的选项为正确答案）
        <el-radio
          class="singlechoice"
          @change="radiochange"
          v-model="radio"
          :label="item.isRight"
          v-for="item in previewobj.options"
          :key="item.id"
        >
          {{ item.title }}
        </el-radio>
      </div>
      <div v-else-if="previewobj.questionType === '2'">
        多选题 选项：（以下选中的选项为正确答案）
        <el-checkbox
          v-for="item in previewobj.options"
          :key="item.id"
          v-model="item.checked"
        >
          {{ item.title }}
        </el-checkbox>
      </div>
      <div v-else></div>
      <hr />
      <div>
        【参考答案】：
        <el-button type="danger" size="small" @click="clickVideo"
          >视频答案预览</el-button
        >
        <div class="video" v-if="videoshow">
          <video
            id="myvideo"
            :src="previewobj.videoURL"
            controls
            autoplay
          ></video>
        </div>
      </div>
      <hr />
      【答案解析】：
      <div v-html="previewobj.answer" style="display: inline-block"></div>
      <hr />
      <div>【题目备注】：{{ previewobj.remarks }}</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="topicDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="topicDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      title="题目审核"
      :visible.sync="auditDialogVisible"
      width="20%"
      @close="auditDialogClose"
    >
      <el-radio v-model="auditObj.chkState" :label="1">通过</el-radio>
      <el-radio v-model="auditObj.chkState" :label="2">拒绝</el-radio>
      <el-input
        type="textarea"
        :rows="2"
        placeholder="请输入审核意见"
        v-model="auditObj.chkRemarks"
        style="margin-top: 20px"
      >
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="auditConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { difficulty, questionType, direction } from "@/api/hmmm/constants";
import { provinces, citys } from "@/api/hmmm/citys.js";
import { simple } from "@/api/hmmm/subjects";
import { simple as secondSimple } from "@/api/hmmm/directorys";
import { simple as tagsSimple } from "@/api/hmmm/tags";
import {
  detail,
  choiceCheck,
  choicePublish,
  remove,
  list,
  choiceAdd,
} from "@/api/hmmm/questions";
import { simple as UserSimple } from "@/api/base/users";
export default {
  data() {
    return {
      searchForm: {
        // 学科
        subjectID: "",
        // 二级目录
        catalogID: "",
        // 难度
        difficulty: "",
        // 试题类型
        questionType: "",
        // 标签
        tags: "",
        // 企业所在地省份
        province: "",
        // 企业所在城市
        city: "",
        // 关键字
        keyword: "",
        // 题目备注
        remarks: "",
        // 企业简称
        shortName: "",
        // 方向
        direction: "",
        // 录入人
        creatorID: "",
      },
      userList: [],
      subjectList: [],
      secondList: [],
      tagsList: [],
      provinceList: [],
      cityList: [],
      questionType,
      difficulty: difficulty,
      direction: direction,
      questionInfo: {
        page: 1,
        pagesize: 5,
      },
      activeName: "first",
      questionList: [],
      total: 0,
      topicDialogVisible: false,
      previewobj: {},
      radio: 1,
      videoshow: false,
      auditDialogVisible: false,
      auditObj: {
        chkState: 1,
        chkRemarks: "",
        id: "",
      },
      putTopicObj: {},
    };
  },
  methods: {
    async delTopic(row) {
      const result = await this.$confirm(
        "此操作将永久删除该题目, 是否继续 ? ",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (result !== "confirm") {
        return this.$message.info("已取消删除");
      }
      await remove(row);
      this.$message.success("删除成功");
      this.getQuestionList();
    },
    auditDialogClose() {
      this.auditObj = {
        chkState: 1,
        chkRemarks: "",
        id: "",
      };
    },
    async auditConfirm() {
      if (this.auditObj.chkRemarks.length > 0) {
        await choiceCheck(this.auditObj);
        this.auditDialogVisible = false;
        this.getQuestionList();
      } else {
        this.$message.warning("请输入审核意见");
      }
    },
    auditTopic(row) {
      this.auditDialogVisible = true;
      this.auditObj.id = row.id;
    },
    topicDialogClose() {
      this.videoshow = false;
      this.previewobj = {};
    },
    clickVideo() {
      this.videoshow = true;
      // const video = document.getElementById('myvideo')
      // video.play()
    },
    checkchange() {
      if (this.previewobj.questionType === "2") {
        this.previewobj.options.forEach((item) => {
          if (item.isRight === 1) {
            item.checked = true;
          } else {
            item.checked = false;
          }
        });
      }
    },
    radiochange() {
      this.radio = 1;
    },
    async getPreviewObj(row) {
      this.topicDialogVisible = true;
      const { data: res } = await detail(row);
      this.previewobj = res;
      // 也可用数组做,数组里存checkbox得值,checkboxgroup+checkbox
      if (this.previewobj.questionType === "2") {
        this.previewobj.options.forEach((item) => {
          if (item.isRight === 1) {
            item.checked = true;
          } else {
            item.checked = false;
          }
        });
      }
    },
    handleCurrentChange(newpage) {
      this.questionInfo.page = newpage;
      this.getQuestionList();
    },
    handleSizeChange(newsize) {
      this.questionInfo.pagesize = newsize;
      this.getQuestionList();
    },
    async handleTabClick(tab, event) {
      delete this.questionInfo.chkState;
      const { data: res } = await this.$store.dispatch(
        "getQuestionList",
        this.questionInfo
      );
      this.questionList = res.items;
      this.total = res.counts;
    },
    async selectChanged(value) {
      const { data: res1 } = await secondSimple(value);
      const { data: res2 } = await tagsSimple(value);
      this.secondList = res1;
      this.tagsList = res2;
    },
    selectListChanged(value) {
      this.searchForm.city = "";
      this.cityList = citys(value);
    },
    async getSubjectList() {
      const { data: res } = await simple();
      this.subjectList = res;
    },
    async getUserList() {
      const { data: res } = await UserSimple();
      this.userList = res;
    },
    async getQuestionList() {
      const { keyword, subjectID } = this.searchForm;
      if (keyword.length > 0) {
        this.questionInfo = { ...this.questionInfo, keyword };
      }
      if ((subjectID + "").length > 0) {
        this.questionInfo = { ...this.questionInfo, subjectID };
      }
      const { data: res } = await list(this.questionInfo);
      this.questionList = res.items;
      this.total = res.counts;
    },
    clear() {
      this.searchForm.province = "";
      this.searchForm.city = "";
      this.questionInfo = {
        page: 1,
        pagesize: 5,
      };
      this.$refs.searchFormRefs.resetFields();
    },
    async addPerfect(row) {
      await choiceAdd({ id: row.id, choiceState: 1 });
      this.getQuestionList();
      this.$message.success("加入精选成功");
    },
  },
  created() {
    this.getQuestionList();
    this.getSubjectList();
    this.getUserList();
    this.provinceList = provinces();
  },
};
</script>

<style scoped lang='less'>
.el-card {
  margin: 15px;
  .q_title {
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
  }
}
.el-select {
  width: 100%;
}
/deep/ .singlechoice .el-radio {
  display: block;
  margin-top: 10px;
  line-height: 23px;
  white-space: normal;
  margin-right: 0;
}

/deep/ .el-checkbox {
  display: block;
  margin-top: 10px;
  line-height: 23px;
  white-space: normal;
  margin-right: 0;
}
.video {
  width: 400px;
  height: 300px;
}
video {
  width: 100%;
  height: 100%;
}
</style>
